package cc.fs.study.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;

import cc.fs.study.R;

/**
 * 头像右下角有认证标志ImageView
 * Created by fostion on 6/3/16.
 */
public class AvatarImageView extends ImageView {

    private int mDrawWidth; // 绘制画布的宽度
    private int mDrawHeight;//绘制画布的高度
    private Paint paint;
    private RectF rectf;
    private boolean isShowAuthIcon; //是否显示认证标志
    private Drawable mDrawable;//认证资源
    private float mIconSize;//认证图标大小
    private float mMarginRight;//离右边距离
    private float mMarginBottom;//离左边距离

    public AvatarImageView(Context context) {
        this(context, null);
    }

    public AvatarImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }

    private void init(AttributeSet attrs) {
        paint = new Paint();
        try {
            //获取自定义属性
            TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.AvatarImageView);
            mIconSize = typedArray.getDimension(R.styleable.AvatarImageView_pSize, 0);
            mMarginRight = typedArray.getDimension(R.styleable.AvatarImageView_pMarginRight, 0);
            mMarginBottom = typedArray.getDimension(R.styleable.AvatarImageView_pMarginBottom, 0);
            isShowAuthIcon = typedArray.getBoolean(R.styleable.AvatarImageView_pShow, true);
            mDrawable = typedArray.getDrawable(R.styleable.AvatarImageView_pScr);
            if (mDrawable == null) {
                isShowAuthIcon = false;
            }
            typedArray.recycle();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void setIsAuth(boolean isAuth) {
        this.isShowAuthIcon = isAuth;
        if (!isAuth) {
            this.isShowAuthIcon = false;
        }
        this.invalidate();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(getDefaultSize(0, widthMeasureSpec), getDefaultSize(0, heightMeasureSpec));
        mDrawWidth = getMeasuredWidth() - getPaddingRight() - (int) mMarginRight;
        mDrawHeight = getMeasuredHeight() - getPaddingBottom() - (int) mMarginBottom;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (isShowAuthIcon && mDrawable != null) {
            try {
                if (rectf == null) {
                    //在右下角固定位置显示
                    rectf = new RectF(mDrawWidth - mIconSize, mDrawHeight - mIconSize, mDrawWidth, mDrawHeight);
                }
                canvas.drawBitmap(((BitmapDrawable) mDrawable).getBitmap(), null, rectf, paint);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
